# Contributor: Michał Polański <michal@polanski.me>
# Maintainer: Michał Polański <michal@polanski.me>
pkgname=rustic
# NOTE: do not move out of the testing repository until upstream says it's no longer beta-quality software
pkgver=0.6.1
pkgrel=1
pkgdesc="Fast, encrypted, deduplicated backups"
url="https://github.com/rustic-rs/rustic"
license="MIT OR Apache-2.0"
arch="all !s390x" # fails to build nix crate
makedepends="
	cargo
	cargo-auditable
	openssl-dev
	zstd-dev
	"
subpackages="
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	"
# NOTE: Keep in sync with rustic_core in Cargo.toml.
_core_ver=0.1.2
source="https://github.com/rustic-rs/rustic/archive/v$pkgver/rustic-$pkgver.tar.gz
	https://github.com/rustic-rs/rustic_core/archive/$_core_ver/rustic_core-$_core_ver.tar.gz
	make-self-update-optional.patch
	fix-abscissa_core-features.patch
	rustic_core-add-tls-native.patch
	build-with-system-tls.patch
	"
options="net" # fetch dependencies

# Disable self-update feature.
_cargo_opts="--no-default-features --frozen"

prepare() {
	mkdir -p vendor
	mv "$srcdir"/rustic_core-$_core_ver vendor/rustic_core

	default_prepare

	# Rust target triple.
	local target=$(rustc -vV | sed -n 's/host: //p')

	# Build against system-provided zstd.
	mkdir -p .cargo
	cat >> .cargo/config.toml <<-EOF
		[target.$target]
		zstd = { rustc-link-lib = ["zstd"] }
	EOF

	cargo fetch --target="$CTARGET" --locked
}

build() {
	cargo auditable build $_cargo_opts --release

	target/release/rustic completions bash > $pkgname.bash
	target/release/rustic completions fish > $pkgname.fish
	target/release/rustic completions zsh > $pkgname.zsh
}

check() {
	cargo test $_cargo_opts
}

package() {
	install -Dm755 target/release/$pkgname -t "$pkgdir"/usr/bin/

	install -Dm644 $pkgname.bash "$pkgdir"/usr/share/bash-completion/completions/$pkgname
	install -Dm644 $pkgname.fish "$pkgdir"/usr/share/fish/vendor_completions.d/$pkgname.fish
	install -Dm644 $pkgname.zsh "$pkgdir"/usr/share/zsh/site-functions/_$pkgname
}

sha512sums="
44b1e2be7336f74e832c33d06101b2ad1ba79123926bd00f3e4884cd49abd75e727e26636451bc393d154ec438fd9168ae9dce8bfa4ec9ffd371b89b6b426dcd  rustic-0.6.1.tar.gz
a3883df06f927e5a0f786e21ae7bec2d2f6090afbd30828bfe8f16bc706cb755688f6d74e5336fc1fce1cbeb750c954e84039084fcfc2fcc1ffcf560f77c576e  rustic_core-0.1.2.tar.gz
f63047855f5e01ea547b921af54e81dc63559e4dee15811e6cfdbf5e503543bb76b0d2612b660c7adff1751e5564186e386145f61f50d0188e5d8215c2586962  make-self-update-optional.patch
ef26d102a32cea39ccd69cd5fe20cc18ef9a5157dbbb1b90ae640922d88c3739947ed2900f6e902ea9583f51be66d32cedddf298b306e3d24a6529d9163da5d1  fix-abscissa_core-features.patch
8d8b40f01ba324987130737b119aaa1a8a330a90263206c7f6eea8c4ba82c35297964a429bc25f87d2a91653f52d1837bca5d305cec684b5aff9df302f2220b7  rustic_core-add-tls-native.patch
e64ba6947ea0eda3074f267fd05e831b9c66850755200233cd155a7c9b184fb0391a3d5199e5fbc1c2fbdc9d47c277aeb2b00df3f530b1c7fc3c0605a7acfc25  build-with-system-tls.patch
"
